<?php

namespace App\Package\Attendance;

/**
 * 间隔日期生成器
 *
 * Date: 22/04/2018
 * @author George
 * @package App\Package\Attendance
 */
class Days
{
    /**
     * @var
     * Date: 21/04/2018
     * @author George
     */
    public $days;

    /**
     * @var
     * Date: 21/04/2018
     * @author George
     */
    protected $start;

    /**
     * @var
     * Date: 21/04/2018
     * @author George
     */
    protected $end;

    /**
     * @var bool
     * Date: 21/04/2018
     * @author George
     */
    public $collection;

    /**
     * 初始化类
     *
     * Days constructor.
     * @param $start
     * @param $end
     * @param $collection
     */
    public function __construct($start, $end, $collection = false)
    {
        $this->days = 0;
        $this->start = $start;
        $this->end = $end;
        $this->collection = $collection ? collect([]) : [];
    }

    /**
     * 获取给定日期范围内的所有日期
     *
     * Date: 22/04/2018
     * @author George
     * @param string $start
     * @param string $end
     * @param bool $collection
     * @return Days
     */
    public static function all(string $start, string $end, bool $collection = false)
    {
        $instance = new self($start, $end, $collection);
        $start_date = strtotime($start);
        $end_date = strtotime($end);
        while ($start_date <= $end_date) {
            $instance->days += 1;
            if ($collection) {
                $instance->collection->push(date('Y-m-d', $start_date));
            } else {
                $instance->collection[] = date('Y-m-d', $start_date);
            }
            $start_date = strtotime('+1 day', $start_date);
        }
        return $instance;
    }
}